package com.scf.loan.Service.impl;


import com.scf.loan.Service.BorrowingsService;
import com.scf.loan.entity.*;
import com.scf.loan.mapper.BorrowingsMapper;
import com.scf.service.HistoryService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BorrowingsServiceImpl implements BorrowingsService {

	@Resource
	private BorrowingsMapper borrwingsMapper;
	@Resource
	private HistoryService historyService;
	@Override
	public List<Borrowings> allAsset(PageBean pageBean) {
		return borrwingsMapper.allAsset(pageBean);
	}

	@Override
	public int counts(PageBean pageBean) {
		int counts = borrwingsMapper.counts(pageBean);
		return counts;
	}

	@Override
	public Borrowers getInfoByBoid(Borrowers borrowers) {

		return borrwingsMapper.getInfoByBoid(borrowers);
	}

	@Override
	public List<Bank> getBankByBoid(Bank boid) {
		return borrwingsMapper.getBankByBoid(boid);
	}

	@Override
	public List<History> allHistory(@Param("plid") Integer plid) {

		return borrwingsMapper.allHistory(plid);
	}

	@Override
	public boolean risksub(Borrowings borrowings) {
		Borrowings borrowingBybwid = borrwingsMapper.getBorrowingBybwid(borrowings);
		if (borrowingBybwid.getStatus() == borrowings.getStatus()) {
			return false;
		} else {
			int risksub = borrwingsMapper.risksub(borrowings);
			if (risksub == 1) {
				return true;
			} else {
				return false;
			}
		}
	}

	@Override
	@GlobalTransactional(rollbackFor = Exception.class)
	public boolean inserthis(History history) {
		com.scf.entity.History history1 = new com.scf.entity.History();
		history1.setAgent(history.getAgent());
		history1.setContent(history.getContent());
		history1.setPlid(history.getPlid());
		history1.setProcessingtime(new Date());
		history1.setSuggestion(history.getSuggestion());
		history1.setStep(history.getStep());
		boolean insert = historyService.insert(history1);
	if(insert){
		return true;
	}
		return false;
	}


	@Override
	public Borrowings getBorrowingByBwid(Borrowings boid) {

		return borrwingsMapper.getBorrowingBybwid(boid);
	}

}